package cn.quickcode.monitor.core;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import cn.quickcode.monitor.ui.provider.SMConsoleProvider;

/**
 * 监控管理器、任务控制器
 * 
 * @author zhangaoxiang
 * @version $Revision$ - $Author$ at $Date$
 */
public class TaskManager
{
	/**
	 * 任务表列
	 */
	public static List<Task<?>> tasks = new ArrayList<Task<?>>();

	/**
	 * 任务线程池服务
	 */
	public static ScheduledExecutorService service = Executors.newScheduledThreadPool(2);

	/**
	 * 通过id获取任务实例
	 * 
	 * @param id
	 * @return
	 */
	public static Task<?> getTask(String taskId)
	{
		for (Task<?> task : tasks)
		{
			if (task.getId().equals(taskId))
				return task;
		}
		return null;
	}
	/**
	 * 通过指的任务id打开任务
	 * @param taskId
	 */
	public static void opentask(String taskId)
	{
		TaskManager.opentask(TaskManager.getTask(taskId));
	}
	/**
	 * 打开任务
	 * 
	 * @param task
	 */
	public static void opentask(Task<?> task)
	{
		SMConsoleProvider.println("Open the Task......");
		SMConsoleProvider.println("Task Id:" + task.getId());
		SMConsoleProvider.println("Task Name:" + task.getName());
		SMConsoleProvider.println("Task will's start at " + task.getInitialDelay() + " later,and Run once every " + task.getDelay() + " " + TimeUnit.MILLISECONDS.name());
		task.setFuture(service.scheduleWithFixedDelay(task, task.getInitialDelay(), task.getDelay(), TimeUnit.MILLISECONDS));
		task.setTaskUnit(TaskUnit.RUNNING);
		SMConsoleProvider.println("Open the Task is done.");
	}

	/**
	 * 打开所有任务
	 */
	public static void opentask()
	{
		for (Task<?> task : tasks)
		{
			if (!task.getTaskUnit().equals(TaskUnit.RUNNING))
			{
				opentask(task);
			}
		}
	}

	/**
	 * 关闭任务
	 * 
	 * @param task
	 */
	public static void shuttask(Task<?> task)
	{
		SMConsoleProvider.println("Shut the Task......");
		SMConsoleProvider.println("Task Id:" + task.getId());
		SMConsoleProvider.println("Task Name:" + task.getName());
		if (task.cancel())
		{
			task.setTaskUnit(TaskUnit.STOP);
			SMConsoleProvider.println("Shut the Task is successful.");
		}
		else
		{
			SMConsoleProvider.println("Shut the Task is failure.");
		}
	}

	/**
	 * 关闭所有任务
	 */
	public static void shuttask()
	{
		for (Task<?> task : tasks)
		{
			if (!task.getTaskUnit().equals(TaskUnit.STOP))
			{
				shuttask(task);
			}
		}
	}

	/**
	 * 关闭指定id的任务
	 * 
	 * @param taskId
	 */
	public static void shuttask(String taskId)
	{
		shuttask(TaskManager.getTask(taskId));
	}

	/**
	 * 关闭线程池
	 */
	public static void shutdown()
	{
		TaskManager.service.shutdown();
	}

	/**
	 * 关闭线程池(立刻)
	 */
	public static void shutdownNow()
	{
		TaskManager.service.shutdownNow();
	}

	/**
	 * 添加任务
	 * 
	 * @param task
	 */
	public static void addTask(Task<?> task)
	{
		if (TaskManager.tasks == null)
		{
			TaskManager.tasks = new ArrayList<Task<?>>();
		}
		if (!TaskManager.tasks.contains(task))
		{
			task.setTaskUnit(TaskUnit.READY);
			TaskManager.tasks.add(task);
		}
	}
	/**
	 * 判断任务是否已经存在
	 * @param taskId
	 * @return
	 */
	public static boolean exists(String taskId)
	{
		for (Task<?> task : tasks)
		{
			if (task.getId().equals(taskId))
				return true;
		}
		return false;
	}
}
